﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using WS.EKA.Business.Dal;
using WS.EKA.Business.Model;
using System.Drawing;
using WS.EKA.UI.Business;

namespace WS.EKA.Web.UI.Pages
{
    public partial class ApplicationManagement : PageBase
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            base.Page_Load(sender,e);
            if (!IsPostBack)
            {
                Bind();
            }
        }

        private List<TB_Application> Source
        {
            get
            {
                if (ViewState["Source"] == null)
                    ViewState["Source"] = ApplicationAccess.GetApplication();
                return ViewState["Source"] as List<TB_Application>;
            }
        }
        private void Bind()
        {
            gvApplication.DataSource = Source;
            gvApplication.DataBind();
        }

        protected void GridView1_RowEditing(object sender, GridViewEditEventArgs e)
        {
            gvApplication.EditIndex = e.NewEditIndex;
            Bind();



        }

        protected void GridView1_RowCancelingEdit(object sender, GridViewCancelEditEventArgs e)
        {
            gvApplication.EditIndex = -1;
            Bind();
        }

        protected void GridView1_RowUpdating(object sender, GridViewUpdateEventArgs e)
        {
            int rowIndex = gvApplication.PageIndex * gvApplication.PageSize + e.RowIndex;
            TB_Application app = Source[rowIndex];
            GridViewRow row = gvApplication.Rows[e.RowIndex];
            TextBox txtAppName = row.Cells[1].FindControl("txtAppName") as TextBox;
            DropDownList ddlIsActive = row.Cells[3].FindControl("ddlIsActive") as DropDownList;
            DropDownList ddlAppType = row.Cells[2].FindControl("ddlAppType") as DropDownList;
            
            app.APP_NAME = txtAppName.Text.Trim();
            app.APP_TYPE = ddlAppType.SelectedValue;
            app.IsActive = ddlIsActive.SelectedValue;
            app.Update_Date = DateTime.UtcNow.ToString("yyyy-MM-dd hh:mm:ss");
            ApplicationAccess.Update(app);

            gvApplication.EditIndex = -1;

            PushToClient(app.APP_TYPE);
            Bind();
           
        }


        protected void gvApplication_RowDataBound(object sender, GridViewRowEventArgs e)
        {
            if (e.Row.RowType == DataControlRowType.DataRow
                && e.Row.RowState.CompareTo(DataControlRowState.Edit) >= 0)
            {
                TB_Application app = e.Row.DataItem as TB_Application;
                DropDownList ddlIsActive = e.Row.Cells[3].FindControl("ddlIsActive") as DropDownList;
                ddlIsActive.SelectedValue = app.IsActive;

                DropDownList ddlAppType = e.Row.Cells[2].FindControl("ddlAppType") as DropDownList;
                ddlAppType.SelectedValue = app.APP_TYPE;
            }
        }

        protected void lbtnCreate_Click(object sender, EventArgs e)
        {
            phCreate.Visible = true;
            ClearValue();
        }

        protected void btnSave_Click(object sender, EventArgs e)
        {
            try
            {
                TB_Application app = new TB_Application();
                app.APP_CODE = txtAppCode.Text.Trim();
                app.APP_NAME = txtAppName.Text.Trim();
                app.APP_TYPE = ddlAppType.SelectedValue;
                app.IsActive = ddlIsActive.SelectedValue;
                ApplicationAccess.Insert(app);
                Source.Add(app);

                PushToClient(app.APP_TYPE);
                Bind();

                phCreate.Visible = false;
                lblMessage.ForeColor = Color.LightGreen;
                lblMessage.Text = "保存成功！";
            }
            catch (Exception ex)
            {
                lblMessage.ForeColor = Color.Red;
                lblMessage.Text = "保存出错！" + ex.Message;
            }
        }

        protected void btnCancel_Click(object sender, EventArgs e)
        {
            phCreate.Visible = false;
        }

        protected void ClearValue()
        {
            txtAppCode.Text = string.Empty;
            txtAppName.Text = string.Empty;
            ddlAppType.SelectedIndex = 0;
            ddlIsActive.SelectedIndex = 0;
            lblMessage.Text = string.Empty;
            
        }

        protected void gvApplication_PageIndexChanging(object sender, GridViewPageEventArgs e)
        {
            gvApplication.PageIndex = e.NewPageIndex;
            Bind();
        }

        protected void PushToClient(string appType)
        {
            ClientDataSourceBridge.Push(WebSocketClientType.Event_List);

            if (appType == "1")
            {
                //1.办公协同
                ClientDataSourceBridge.Push(WebSocketClientType.Event_OfficeAppStatus);
            }
            else if (appType == "2")
            {
                //2.管理支撑
                ClientDataSourceBridge.Push(WebSocketClientType.Event_ManageAppStatus);
            }
            else if (appType == "3")
            {
                //3.营运支撑
                ClientDataSourceBridge.Push(WebSocketClientType.Event_OperationAppStatus);
            }
            else if (appType == "4")
            {
                //4.基础应用
                ClientDataSourceBridge.Push(WebSocketClientType.Event_FoundationAppStatus);
            }

        }
    }
}